const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: {},
    isGoBotton: false,
    logspages: 1,
    title: '',
    islogin: false,
    isadmin: false,
    voteuser: {}
  },
  change(e: any) {
    this.setData({
      title: e.detail.value
    })
  },
  find() {
    let list = {};
    let that = this;
    wx.request({
      url: 'http://localhost:9090/vote-subject/index?title=' + that.data.title,
      method: 'GET',
      success(res: any) {
        list = res.data.list;
        that.setData({ list, isGoBotton: false });
      }
    });
  },
  add() {
    wx.navigateTo({ url: '../add/add' })
  },
  editsubject(e: any) {
    let arr = e.target.dataset.arr;
    let vsid = e.target.dataset.vsid;
    const currentTime = this.formatDate(new Date());
    if (currentTime >= e.target.dataset.starttime) {
      wx.showToast({ title: '投票已经开始', icon: 'error' });
      return;
    }
    if (arr.length > 0) {
      wx.showToast({ title: '投票已经开始', icon: 'error', });
      return;
    }
    wx.navigateTo({ url: '../add/add?vsid=' + vsid })
  },
  contains(e: any) {
    if (!this.data.islogin) {
      this.gologin();
      return;
    }
    const currentTime = this.formatDate(new Date());
    if (currentTime < e.target.dataset.starttime) {
      wx.showToast({ title: '暂未开放', icon: 'error' });
      return;
    }
    let arr = e.target.dataset.arr;
    let userid = e.target.dataset.userid;
    let votetype = e.target.dataset.votetype;
    let vsid = e.target.dataset.vsid;
    console.log(vsid);

    let flag = false;
    for (let index = 0; index < arr.length; index++) {
      if (arr[index].vuUserId == userid) {
        flag = true;
        break;
      }
    }
    console.log(votetype);
    if (flag && votetype == 1) wx.showToast({ title: '您已参与过了', icon: 'error', });
    else if (votetype == 2 || votetype == 11) {
      wx.request({
        url: 'http://localhost:9090/vote-subject/findCountVote',
        method: 'GET',
        data: { userid, vsid },
        success(res: any) {
          if (res.data < 1) wx.navigateTo({ url: '../vote/vote?vsid=' + e.target.dataset.vsid })
          else wx.showToast({ title: '今日已参与', icon: 'error', });
        }
      });
    }
    else if (votetype == 3 || votetype == 21) {
      wx.request({
        url: 'http://localhost:9090/vote-subject/findCountVote',
        method: 'GET',
        data: { userid, vsid },
        success(res: any) {
          if (res.data < 3) wx.navigateTo({ url: '../vote/vote?vsid=' + e.target.dataset.vsid })
          else wx.showToast({ title: '今日已达上限', icon: 'error', });
        }
      });
    }
    else wx.navigateTo({ url: '../vote/vote?vsid=' + e.target.dataset.vsid })
  },
  goview(e: any) {
    wx.navigateTo({ url: '../view/view?vsid=' + e.target.dataset.vsid })
  },
  gologin() {
    wx.navigateTo({ url: '../login/login' })
  },
  formatDate(date: Date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(app.globalData.isadmin);
    this.setData({
      voteuser: app.globalData.voteuser,
      islogin: app.globalData.islogin,
      title: app.globalData.title,
      isadmin: app.globalData.isadmin
    });
    this.setData({
      title: ''
    })
    if (app.globalData.title != '') {
      this.setData({ title: app.globalData.title });
      this.find();
    }
    app.globalData.title = '';
    this.find();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isGoBotton) {
      let pageindex = this.data.logspages;
      wx.showLoading({
        title: '正在加载'
      });
      setTimeout(() => {
        if (pageindex * 10 >= this.data.list.records.length) {
          wx.showModal({
            title: "提示",
            content: "已经没有更多内容了",
            showCancel: false
          });
          this.setData({
            isGoBotton: true
          })
        } else {
          this.setData({
            logspages: pageindex + 1,
          });
        }
        wx.hideLoading();
      }, 300);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})